<!DOCTYPE html>
<html>
<head>
    <title>region form</title>
    <#include "/header.html">
</head>
<body class="bg-white">
<div id="appMain">
    <form class="layui-form layuimini-form" action="" lay-filter="dataForm">
        <div class="layui-form-item">
            <label class="layui-form-label">企业名称</label>
            <div class="layui-input-block">
                <input type="text" name="enterpriseName" lay-verify="required" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工作简介</label>
            <div class="layui-input-block">
                <input type="text" name="introduction" lay-verify="required" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工作要求</label>
            <div class="layui-input-block">
                <input type="text" name="requirement" lay-verify="required" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">省</label>
            <div class="layui-input-block">
                <select id="province" name="province" lay-filter="province" lay-search>
      			</select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">市</label>
            <div class="layui-input-block">
                <select id="city" name="city" lay-filter="city" lay-search>
      			</select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">区</label>
            <div class="layui-input-block">
                <select id="area" name="area" lay-filter="area" lay-search>
      			</select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">详细地址</label>
            <div class="layui-input-block">
                <input type="text" name="address" lay-verify="required" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">关键词</label>
            <div class="layui-form-mid tag" lay-filter="keywords" lay-allowclose="true" lay-newTag="true" id="keywords">
                <!--<button type="button" class="tag-item">网站设置</button>-->
            </div>
        </div>
      	<div class="layui-form-item">
            <label class="layui-form-label">兼职类型</label>
            <div class="layui-input-block">
                <select id="categoryId" name="categoryId" lay-search>
      			</select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">薪酬</label>
            <div class="layui-input-block">
                <input type="text" name="pay" placeholder="请输入整数" lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">薪酬类型</label>
            <div class="layui-input-block">
                <input type="radio" name="payType" value="0" title="小时" checked>
                <input type="radio" name="payType" value="1" title="天">
                <input type="radio" name="payType" value="2" title="月">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">结算方式</label>
            <div class="layui-input-block">
                <input type="radio" name="settlement" value="1" title="日结" checked>
                <input type="radio" name="settlement" value="2" title="月结">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">招聘人数</label>
            <div class="layui-input-block">
                <input type="text" name="numberPeople" lay-verify="required" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">集合时间</label>
            <div class="layui-input-block">
            <input type="text" name="musterTime" id="datetime" lay-verify="required" lay-reqtext="请选择时间" placeholder="请选择时间" autocomplete="off" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">终止报名时间</label>
            <div class="layui-input-block">
                <input type="text" name="endTime" id="datetime2" lay-verify="required" lay-reqtext="请选择时间" placeholder="请选择时间" autocomplete="off" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上班时间</label>
            <div class="layui-input-block">
                <input type="text" name="toWorkTime" id="date" lay-verify="required" lay-reqtext="请选择时间" placeholder="请选择时间" autocomplete="off" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">下班时间</label>
            <div class="layui-input-block">
                <input type="text" name="offWorkTime" id="date2" lay-verify="required" lay-reqtext="请选择时间" placeholder="请选择时间" autocomplete="off" class="layui-input"/>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="0" title="禁用">
                <input type="radio" name="status" value="1" title="发布中" checked="">
                <input type="radio" name="status" value="2" title="已过期">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发布类型</label>
            <div class="layui-input-block">
                <input type="radio" name="releaseType" value="0" title="直发" checked="">
                <input type="radio" name="releaseType" value="1" title="转发">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系方式</label>
            <div class="layui-input-block">
                <input type="text" name="information" id="information" placeholder="直发不需要填写" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" id="commitBtn" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    layui.use(['form', 'laydate', 'tag', 'dtree', 'laytpl'], function () {
        var form = layui.form,
	        tag = layui.tag,
	        dtree = layui.dtree,
	        laytpl = layui.laytpl,
            $ = layui.$;
        var laydate = layui.laydate;
        var keywords = [];

        var _parent = parent;
        /* dtree.render({
            elem: "#menuTree",
            data: _parent.menuTreeData,
            icon: "-1",
            selectInputName: {
                nodeId: "parentId",
                context: "parentName"
            },
            // initLevel: 3,
            select: true, //指定下拉树模式
            done: function(res, $ul, first){
                if(first && _parent.formDataId > 0) {
                    $.ajax({
                        type: "POST",
                        url: serverUrl('app/job/info/' + _parent.formDataId),
                        success: function (result) {
                            if (result.code === 0) {
                                form.val('dataForm', result.info);
                                dtree.dataInit("menuTree", result.info.parentId);
                                dtree.selectVal("menuTree");
                                $('#uploadPreview').attr('src', result.info.image); //url
                                form.render();

                                $(result.info.keyword).each(function (i, v) {
                                    keywords.push(v);
                                });

                                laytpl($("#tagsTpl").html()).render(keywords, function(html){
                                    $("#keywords").html(html);
                                });
                                tag.render();
                                tag.on('add(keywords)', function (data) {
                                    keywords.push($(data.othis).text());
                                    console.log(keywords);
                                });
                                tag.on('delete(keywords)', function (data) {
                                    keywords.splice(data.index, 1);
                                    console.log(keywords);
                                });
                            }
                        }
                    });
                }
            }
        }); */
        

    	form.on("select(province)",function(data){
    		$.ajax({
    			url:serverUrl("region/city"),
    			type:"post",
    			data:{"id":data.value},
    			dataType:"",
    			success:function(btn){
  		          var select = $("#city");
		        	select.empty();
  			      	for (var j = 0; j < btn.count; j++) {
  				      	  select.append("<option value='"+btn.data[j].id+"'>"+btn.data[j].name+"</option>");
  			  		}
  		          layui.form.render("select");
  	    		$.ajax({
  	    			url:serverUrl("region/area"),
  	    			type:"post",
  	    			data:{"id":btn.data[0].id},
  	    			dataType:"",
  	    			success:function(btn){
  	  		          var select = $("#area");
  	  		        	select.empty();
  	  			      	for (var j = 0; j < btn.count; j++) {
  	  				      	  select.append("<option value='"+btn.data[j].id+"'>"+btn.data[j].name+"</option>");
  	  			  		}
  	  		          layui.form.render("select");
  	    			},
  	    			error:function(){
  	    				console.log("获取失败");
  	    			}
  	    		})
    			},
    			error:function(){
    				console.log("获取失败");
    			}
    		})
    	})
    	form.on("select(city)",function(data){
    		$.ajax({
    			url:serverUrl("region/area"),
    			type:"post",
    			data:{"id":data.value},
    			dataType:"",
    			success:function(btn){
  		          var select = $("#area");
  		        	select.empty();
  			      	for (var j = 0; j < btn.count; j++) {
  				      	  select.append("<option value='"+btn.data[j].id+"'>"+btn.data[j].name+"</option>");
  			  		}
  		          layui.form.render("select");
    			},
    			error:function(){
    				console.log("获取失败");
    			}
    		})
    	})
        //获取类型select
    	  $.ajax({
    			url:serverUrl("job/categoryallname"),
    			type:"post",
    			dataType:"",
    			success:function(btn){
    		          var select = $("#categoryId");
    			      	for (var j = 0; j < btn.count; j++) {
    				      	  select.append("<option value='"+btn.data[j].id+"'>"+btn.data[j].name+"</option>");
    			  		}
    		          layui.form.render("select");
    			},
    			error:function(){
    				console.log("获取失败");
    			}
    		})
			console.log(_parent);
    		if(_parent.formDataId == 0){//添加时
    		//获取省级select
      	  	  $.ajax({
  	  			url:serverUrl("region/province"),
  	  			type:"post",
  	  			dataType:"",
  	  			success:function(btn){
  	  		          var select = $("#province");
  	  			      	for (var j = 0; j < btn.count; j++) {
  	  				      	  select.append("<option value='"+btn.data[j].id+"'>"+btn.data[j].name+"</option>");
  	  			  		}
  	  		          layui.form.render("select");
  	  		          //获取城市
  	  		        $.ajax({
  	  	    			url:serverUrl("region/city"),
  	  	    			type:"post",
  	  	    			data:{"id":btn.data[0].id},
  	  	    			dataType:"",
  	  	    			success:function(btn){
  	  	  		          var select = $("#city");
  	  			        	select.empty();
  	  	  			      	for (var j = 0; j < btn.count; j++) {
  	  	  				      	  select.append("<option value='"+btn.data[j].id+"'>"+btn.data[j].name+"</option>");
  	  	  			  		}
  	  	  		          layui.form.render("select");
      	  		          //获取地区
  	  	  	    		$.ajax({
  	  	  	    			url:serverUrl("region/area"),
  	  	  	    			type:"post",
  	  	  	    			data:{"id":btn.data[0].id},
  	  	  	    			dataType:"",
  	  	  	    			success:function(btn){
  	  	  	  		          var select = $("#area");
  	  	  	  		        	select.empty();
  	  	  	  			      	for (var j = 0; j < btn.count; j++) {
  	  	  	  				      	  select.append("<option value='"+btn.data[j].id+"'>"+btn.data[j].name+"</option>");
  	  	  	  			  		}
  	  	  	  		          layui.form.render("select");
  	  	  	    			},
  	  	  	    			error:function(){
  	  	  	    				console.log("获取失败");
  	  	  	    			}
  	  	  	    		})
  	  	    			},
  	  	    			error:function(){
  	  	    				console.log("获取失败");
  	  	    			}
	  	  	    		})
  	  			},
  	  			error:function(){
  	  				console.log("类型获取失败");
  	  			}
  	  		})      
    		}else{//编辑时
    			$.ajax({
      	  			url:serverUrl("region/province"),
      	  			type:"post",
      	  			dataType:"",
      	  			success:function(btn){
      	  		          var select = $("#province");
      	  			      	for (var j = 0; j < btn.count; j++) {
      	  			      		if(btn.data[j].id==_parent.province){
        	  				      	  select.append("<option value='"+btn.data[j].id+"' selected>"+btn.data[j].name+"</option>");
      	  			      		}else{
      	    				      select.append("<option value='"+btn.data[j].id+"'>"+btn.data[j].name+"</option>");
      	  			      		}
      	  			  		}
      	  		          layui.form.render("select");
	      	  		},
	  	  			error:function(){
	  	  				console.log("类型获取失败");
	  	  			}
    			})
  		          //获取地区
	  	    		$.ajax({
	  	    			url:serverUrl("region/parentid/byid"),
	  	    			type:"post",
	  	    			data:{"id":_parent.area},
	  	    			dataType:"",
	  	    			success:function(btn){
	  	  		          var select = $("#area");
	  	  		        	select.empty();
	  	  			      	for (var j = 0; j < btn.count; j++) {
      	  			      		if(btn.data[j].id==_parent.area){
      	  				      	  select.append("<option value='"+btn.data[j].id+"' selected>"+btn.data[j].name+"</option>");
    	  			      		}else{
    	    				      select.append("<option value='"+btn.data[j].id+"'>"+btn.data[j].name+"</option>");
    	  			      		}
	  	  			  		}
	  	  		          layui.form.render("select");
	  	    			},
	  	    			error:function(){
	  	    				console.log("获取失败");
	  	    			}
	  	    		})
  	  		          //获取城市
  	  		        $.ajax({
  	  	    			url:serverUrl("region/parentid/byid"),
  	  	    			type:"post",
  	  	    			data:{"id":_parent.city},
  	  	    			dataType:"",
  	  	    			success:function(btn){
  	  	  		          var select = $("#city");
  	  			        	select.empty();
  	  	  			      	for (var j = 0; j < btn.count; j++) {
      	  			      		if(btn.data[j].id==_parent.city){
        	  				      	  select.append("<option value='"+btn.data[j].id+"' selected>"+btn.data[j].name+"</option>");
      	  			      		}else{
      	    				      select.append("<option value='"+btn.data[j].id+"'>"+btn.data[j].name+"</option>");
      	  			      		}
  	  	  			  		}
  	  	  		          layui.form.render("select");
	  	    			},
	  	    			error:function(){
	  	    				console.log("获取失败");
	  	    			}
	  	    		})
    		}
    	
        laydate.render({
            elem: '#date',
            type: 'time'
          });
        laydate.render({
              elem: '#date2',
              type: 'time'
            });
        laydate.render({
            elem: '#datetime',
            type: 'datetime'
          });
        laydate.render({
            elem: '#datetime2',
            type: 'datetime'
          });
        /* tag.on('click(keywords)', function (data) {
            console.log('点击');
            console.log(data.index); //得到当前Tag的所在下标
        }); */

        tag.on('add(keywords)', function (data) {
            console.log('新增');
            console.log(data.index); //得到当前Tag的所在下标
            console.log(data.othis); //得到新增的DOM对象
            keywords.push($(data.othis).text());
        });

        tag.on('delete(keywords)', function (data) {
            console.log('删除');
            console.log(data.index); //得到当前Tag的所在下标
            console.log(data.elem); //得到当前的Tag大容器
            keywords.splice(data.index, 1);
        });
        $(document).ready(function () {
            if (parent.formDataId > 0) {
                $.ajax({
                    type: "POST",
                    url: serverUrl('job/info/' + parent.formDataId),
                    data: {},
                    dataType: "json",
                    success: function (result) {
                        if (result.code === 0) {
                            form.val('dataForm', result.info);
                        }
                    }
                });
            }
        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
        	if(data.field.releaseType==1&&data.field.information==""){
        		alert("转发需要填写联系方式");
        		return false;
        	}
            /*alert(JSON.stringify(data.field));
            return false;*/
            var params = data.field;
            params['id'] = _parent.formDataId || null;
            params['label'] = keywords;
            
            var method = _parent.formDataId > 0 ? 'update' : 'save';
            submitFormData('job/'+method, params, function (res) {
                
            });
			submitFormData('job/information', {'information':data.field.releaseType+":"+data.field.information});
            return false;
        });

    });
</script>
</body>
</html>